package cn.danger.com.widget;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import cn.danger.com.activity.R;
import cn.danger.com.util.DensityUtil;

/**
 * Created by 俊 on 2016/12/28.
 */
public class MyPopupWindow extends PopupWindow {

    private View view;
    private int width;
    private int height;
    private FrameLayout layout1;
    private FrameLayout layout2;
    private FrameLayout layout3;
    private TextView item1_textView;
    private TextView item2_textView;

    public MyPopupWindow(Context context) {
        calWidthAndHeight(context);

        view = LayoutInflater.from(context).inflate(R.layout.popup_window_layout, null);

        setContentView(view);
        setWidth(width);
        setHeight(height);
        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new BitmapDrawable());
        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    dismiss();
                    return true;
                }
                return false;
            }
        });

        bindView();
        bindEvent();
    }

    private void bindView() {
        layout1 = (FrameLayout) view.findViewById(R.id.layout1);
        layout2 = (FrameLayout) view.findViewById(R.id.layout2);
        layout3 = (FrameLayout) view.findViewById(R.id.layout3);
        item1_textView = (TextView) view.findViewById(R.id.item1_textView);
        item2_textView = (TextView) view.findViewById(R.id.item2_textView);
    }

    private void bindEvent() {
        layout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item1Listener.onClick();
            }
        });
        layout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item2Listener.onClick();
            }
        });
        layout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setItem1TextView(String item1) {
        if (item1_textView != null) {
            item1_textView.setText(item1);
        }
    }

    public void setItem2TextView(String item2) {
        if (item2_textView != null) {
            item2_textView.setText(item2);
        }
    }

    private OnClickItem1Listener item1Listener;

    public interface OnClickItem1Listener {
        void onClick();
    }

    public void setOnClickItem1Listener(OnClickItem1Listener item1Listener) {
        this.item1Listener = item1Listener;
    }

    private OnClickItem2Listener item2Listener;

    public interface OnClickItem2Listener {
        void onClick();
    }

    public void setOnClickItem2Listener(OnClickItem2Listener item2Listener) {
        this.item2Listener = item2Listener;
    }

    /**
     * 计算popupWindow的宽度和高度
     *
     * @param context
     */
    private void calWidthAndHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);

        width = outMetrics.widthPixels;
        height = DensityUtil.dip2px(context, 153);
    }

}
